using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using community;

namespace Service
{
    public class Cls_DBHelper
    {
        
        public SqlConnection m_connection;
        public  SqlConnection m_Connection
        {
            get
            {
                //Cls_DBHelper m_Clshelper = new Cls_DBHelper();
                //string m_str_connectionString = "server=(local);database=community;trusted_connection=yes";
                string m_str_connectionString = SQL.conStr;
                //string m_str_connectionString = ConfigurationManager.AppSettings["conn"];
                if (m_connection == null)
                {
                    m_connection = new SqlConnection(m_str_connectionString);
                    m_connection.Open();
                }
                else if (m_connection.State == System.Data.ConnectionState.Closed)
                {
                    m_connection.Open();
                }
                else if (m_connection.State == System.Data.ConnectionState.Broken)
                {
                    m_connection.Close();
                    m_connection.Open();
                }
                return m_connection;
            }
        }

        public int m_intExecuteCommand(params SqlParameter[] m_valuesArr)
        {
            Cls_DBHelper m_Clshelper = new Cls_DBHelper();
            SqlCommand m_cmd = new SqlCommand();
            m_cmd.Connection = m_Clshelper.m_Connection;
            m_cmd.CommandText = "Pro_UpdateBooksCatagory";
            m_cmd.CommandType = CommandType.StoredProcedure;
            m_cmd.Parameters.AddRange(m_valuesArr);
            int intResult = m_cmd.ExecuteNonQuery();
            return intResult;
        }

        public  int m_intExecuteCommand(string m_strSafeSql)
        {
            Cls_DBHelper m_Clshelper = new Cls_DBHelper();
            SqlCommand m_cmd = new SqlCommand(m_strSafeSql, m_Clshelper.m_Connection);
            int intResult = m_cmd.ExecuteNonQuery();
            return intResult;
        }

        public int m_intExecuteCommand(string m_strSql, params SqlParameter[] m_clsValuesArr)
        {
            Cls_DBHelper m_Clshelper = new Cls_DBHelper();
            SqlCommand m_cmd = new SqlCommand(m_strSql, m_Clshelper.m_Connection);
            m_cmd.Parameters.AddRange(m_clsValuesArr);
            int intResult = m_cmd.ExecuteNonQuery();
            return intResult;
        }

        public int m_intGetScalar(string m_strSafeSql)
        {
            Cls_DBHelper m_Clshelper = new Cls_DBHelper();
            SqlCommand m_cmd = new SqlCommand(m_strSafeSql, m_Clshelper.m_Connection);
            int intResult = Convert.ToInt32(m_cmd.ExecuteScalar());
            return intResult;
        }

        public int m_intGetScalar(params SqlParameter[] m_clsValuesArr)
        {
            Cls_DBHelper m_Clshelper = new Cls_DBHelper();
            SqlCommand m_cmd = new SqlCommand();
            m_cmd.Connection = m_Clshelper.m_Connection;
            m_cmd.CommandText = "Pro_InsertOrder";
            m_cmd.CommandType = CommandType.StoredProcedure;
            m_cmd.Parameters.AddRange(m_clsValuesArr);
            int intResult = Convert.ToInt32(m_cmd.ExecuteScalar());
            return intResult;
        }

        public int m_intGetScalar(string m_strSql, params SqlParameter[] m_clsValuesArr)
        {
            Cls_DBHelper m_Clshelper = new Cls_DBHelper();
            SqlCommand cmd = new SqlCommand(m_strSql, m_Clshelper.m_Connection);
            cmd.Parameters.AddRange(m_clsValuesArr);
            int intResult = Convert.ToInt32(cmd.ExecuteScalar());
            return intResult;
        }

        public SqlDataReader m_clsGetReader(string m_strSafeSql)
        {
            Cls_DBHelper m_Clshelper = new Cls_DBHelper();
            SqlCommand m_cmd = new SqlCommand(m_strSafeSql, m_Clshelper.m_Connection);
            SqlDataReader ClsReader = m_cmd.ExecuteReader();
            return ClsReader;
        }

        public SqlDataReader m_clsGetReader(string strSql, params SqlParameter[] m_clsValuesArr)
        {
            Cls_DBHelper m_Clshelper = new Cls_DBHelper();
            SqlCommand m_cmd = new SqlCommand(strSql, m_Clshelper.m_Connection);
            m_cmd.Parameters.AddRange(m_clsValuesArr);
            SqlDataReader clsReader = m_cmd.ExecuteReader();
            return clsReader;
        }

        public DataTable m_clsGetDataSet(string strSafeSql)
        {
            Cls_DBHelper m_Clshelper = new Cls_DBHelper();
            DataSet clsDs = new DataSet();
            SqlCommand m_cmd = new SqlCommand(strSafeSql, m_Clshelper.m_Connection);
            SqlDataAdapter clsDa = new SqlDataAdapter(m_cmd);
            clsDa.Fill(clsDs);
            return clsDs.Tables[0];
        }

        public DataTable m_clsGetDataSet(string strSql, params SqlParameter[] m_clsValuesArr)
        {
            Cls_DBHelper m_Clshelper = new Cls_DBHelper();
            DataSet clsDs = new DataSet();
            SqlCommand m_cmd = new SqlCommand(strSql, m_Clshelper.m_Connection);
            m_cmd.Parameters.AddRange(m_clsValuesArr);
            SqlDataAdapter da = new SqlDataAdapter(m_cmd);
            da.Fill(clsDs);
            return clsDs.Tables[0];
        }
    }
}
